package com.example.warehouse.service;

import com.example.warehouse.dto.InventoryReportDTO;
import com.example.warehouse.mapper.InventoryLogMapper;
import com.example.warehouse.mapper.InventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class InventoryService {
    @Autowired
    private InventoryMapper  inventoryMapper;

    @Autowired
    private InventoryLogMapper inventoryLogMapper;

    private static final int MIN_STOCK_THRESHOLD = 10;  // 库存预警阈值


    // 根据商品ID查询库存数量
    public Integer getInventoryByProductId(Long productId) {
        // 调用 DAO 层获取库存数据
        return inventoryMapper.getInventoryByProductId(productId);
    }


    //查询所有库存预警
    public String checkInventoryWarnings(Long productId){
     //调用Mapper获取库存预警状态
        Integer warning = inventoryMapper.getInventoryWarningByProductId(productId, MIN_STOCK_THRESHOLD);
        //判断库存是否低于预警阈值
        if(warning != null && warning == 1){
            return "商品ID" + productId + "库存低于预警阈值";
        }

        return  "商品ID" + productId + "库存正常";
    }


//    获取日报表
    public List<InventoryReportDTO> generateDailyReport(LocalDate date) {
        return inventoryMapper.getInventoryReportByDate(date);
    }
//    获取月报表
    public List<InventoryReportDTO> generateMonthlyReport(LocalDate startDate, LocalDate endDate) {
        return inventoryMapper.getInventoryReportByMonth(startDate, endDate);
    }
//    获取年报表
    public List<InventoryReportDTO> generateAnnualReport(LocalDate startDate, LocalDate endDate) {
        return inventoryMapper.getInventoryReportByYear(startDate, endDate);
    }


}
